import { Controller, Get, Inject, Query } from '@nestjs/common';
import { Observable } from 'rxjs';
import { AppService } from './app.service';
import { ClientOptions, ClientProxy, ClientProxyFactory, Transport } from '@nestjs/microservices';

@Controller()
export class AppController {
    private readonly redisServer: ClientProxy;
    constructor(private appService: AppService, @Inject('HTTP_SERVICE') private readonly httpServer: ClientProxy) {
        this.redisServer = ClientProxyFactory.create({
            transport: Transport.REDIS,
            options: {
                host: process.env.REDIS_HOST || 'localhost',
                port: Number(process.env.REDIS_PORT) || 6379,
            },
        } as ClientOptions);
    }

    @Get('/redis_server')
    getRedisData(@Query() param): Observable<any> {
        console.log(process.env.REDIS_HOST);

        return this.redisServer.send({ cmd: 'getRedisData' }, param);
    }

    @Get('/http_server')
    getHttpData(@Query() param): Observable<any> {
        return this.httpServer.send({ cmd: 'getHttpData' }, param);
    }

    @Get('/hello')
    async getHello() {
        console.log(process.env.MYSQL_HOST);

        return await this.appService.getHello();
    }

    @Get('/insert')
    async insertInfo(@Query() param) {
        return await this.appService.insertInfo(param);
    }
}
